package com.knav.inject.RecyleViewInject;

import android.app.Activity;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;

import androidx.recyclerview.widget.RecyclerView;

import com.knav.inject.Exception.InjectRegisterException;
import com.knav.inject.Exception.NoSuchFindException;

import java.lang.reflect.Field;


public class RegisterRecyclerView {


    private static RegisterRecyclerView registerRecyclerView;
    private Class<? extends RecyclerView.Adapter> recyclerViewClazz;

    public static RegisterRecyclerView getInstance() {
        if (registerRecyclerView == null) {
            registerRecyclerView = new RegisterRecyclerView();
        }
        return registerRecyclerView;
    }

    public void RegisterRecyclerView(Activity activity, Class<? extends RecyclerView.Adapter> recyclerViewClazz) {
        this.recyclerViewClazz = recyclerViewClazz;
        if (recyclerViewClazz == null) {
            throw new InjectRegisterException("current RecyclerView inject Exception");
        }

        ViewHolder viewHolder = this.recyclerViewClazz.getAnnotation(ViewHolder.class);
        if (viewHolder == null) {
            throw new InjectRegisterException("ViewHolder 没注入");
        }
        Field[] fields = this.recyclerViewClazz.getFields();
//        for (Field field : fields) {
//            Log.e("TAG", "RegisterRecyclerView11: " + field.getType().getName());
//        }
        View ItemLoadView = null;
        try {

            int parentId = viewHolder.ParentId();

            ViewGroup parent = (ViewGroup) activity.getLayoutInflater().inflate(parentId, null);
            ItemLoadView = activity.getLayoutInflater().inflate(viewHolder.layoutId(), parent, false);

        } catch (Exception e) {
            e.printStackTrace();
        }
        if (!fields[fields.length - 1].getType().getName().equals("android.view.View")) {
            throw new IllegalArgumentException("ViewHolder 注入第一个字段必须时View类型 ");
        } else {
            fields[fields.length - 1].setAccessible(true);
            try {
                if (ItemLoadView != null) {
                    fields[fields.length - 1].set(fields[fields.length - 1], ItemLoadView);
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

        try {
            for (int i = 0; i < fields.length; i++) {

                FindItem annotation = fields[i].getAnnotation(FindItem.class);
                if (annotation == null && i != fields.length - 1) {
//                    Log.e("TAG", "RegisterRecyclerView: use");
                    throw new NoSuchFindException("you not FindItem annotate");
                } else {
                    if (annotation == null){
                        continue;
                    }
                    int itemId = annotation.itemId();
                    fields[i].set(fields[i], ItemLoadView.findViewById(itemId));
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


}
